package com.sense.kukoo.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;

public class NetworkTypeUtils {

    /**
     * 没有连接网络
     */
    private static final String NETWORK_NONE = "null";
    /**
     * 移动网络
     */
   public static final String NETWORK_MOBILE = "MOBILE";
    /**
     * 无线网络
     */
    public static final String NETWORK_WIFI = "WIFI";
    /**
     * 2G网络
     */
    public static final String NETWORK_2G = "2G";
    /**
     * 3G网络
     */
    public static final String NETWORK_3G = "3G";
    /**
     * 4G网络
     */
    public static final String NETWORK_4G = "4G";
    /**
     * 未知
     */
    public static final String NETWORK_UNKNOW = "未知";
    /**
     * GPRS
     */
    public static final String GPRS = "GPRS";
    /**
     * EDGE
     */
    public static final String EDGE = "EDGE";
    /**
     * UMTS
     */
    public static final String UMTS = "UMTS";
    /**
     * HSDPA
     */
    public static final String HSDPA = "HSDPA";
    /**
     * HSUPA
     */
    public static final String HSUPA = "HSUPA";
    /**
     * HSPA
     */
    public static final String HSPA = "HSPA";
    /**
     * CDMA
     */
    public static final String CDMA = "CDMA";
    /**
     * EVDO_0
     */
    public static final String EVDO_0 = "EVDO_0";
    /**
     * EVDO_A
     */
    public static final String EVDO_A = "EVDO_A";
    /**
     * 1xRTT
     */
    public static final String XRTT = "1xRTT";

    /**
     * 定义电话管理器对象
     */
    public static TelephonyManager mTelephonyManager;
    /**
     * 定义连接管理器对象
     */
    public static ConnectivityManager mConnectivityManager;
    /**
     * 定义网络信息对象
     */
    public static NetworkInfo mNetworkInfo;

    /**
     * 得到网络类型
     *
     * @param context
     * @return 网络类型
     */

    public static String getNetWrokType(Context context) {
        // 得到连接管理器对象
        mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (mConnectivityManager != null) {
            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        }

        if (mNetworkInfo != null && mNetworkInfo.isConnected()) {

            if (mNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (mNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        }
        return NETWORK_NONE;
    }

    /**
     *<br> Description: 是否使用VPN
     *<br> Author:      chenhuanwei
     *<br> Date:        2018/6/4 21:33
     */
    public static boolean isUsingVPN(Context context) {
        try {
            Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces();
            if (niList != null) {
                for (NetworkInterface intf : Collections.list(niList)) {
                    if (!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
                        continue;
                    }
                    if ("tun0".equals(intf.getName()) || "ppp0".equals(intf.getName())) {
                        return true; // The VPN is up
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 判断是否连接上
     *
     * @return
     */
    public static final boolean isNetConnected() {
        if (mNetworkInfo != null && mNetworkInfo.isConnected()) {
            return true;
        }
        return false;
    }

//    /**
//     * 获取移动网络的类型
//     *
//     * @param context
//     * @return 移动网络类型
//     */
//    public static final String getMobileNetType(Context context) {
//
//        mTelephonyManager = (TelephonyManager) context
//                .getSystemService(Context.TELEPHONY_SERVICE);
//        int networkType = 0;
//        if (mTelephonyManager != null) {
//            networkType = mTelephonyManager.getNetworkType();
//        }
//
//        return getNetworkClass(networkType);
//    }

    /**
     * 判断移动网络的类型
     *
     * @param networkType
     * @return 移动网络类型
     */
    private static final String getNetworkClass(int networkType) {
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return GPRS;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return EDGE;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return CDMA;
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return XRTT;
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_2G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return UMTS;
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return EVDO_0;
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return EVDO_A;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return HSDPA;
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return HSUPA;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return HSPA;
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_3G;
            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_4G;
            default:
                return NETWORK_UNKNOW;
        }
    }
}
